<?php
	if(isset($_GET['id']))
	{
		$user_id = $_COOKIE['studialuserid'];
		$friend_id = $_GET['id'];
		
		@$connect = mysql_pconnect('localhost', 'root', '');
		if(!$connect)
		{
			echo "Không thể kết nối đến cơ sở dữ liệu, Bạn vui lòng thử lại sau";
			exit;	
		}
		mysql_select_db('studial');
		$query = "select * from `friends` where `user_id` = '$user_id' AND `friend_id` = '$friend_id';";
		$result = mysql_query($query);
		$check_1 = mysql_num_rows($result);
		$query = "select * from `friends` where `user_id` = '$friend_id' AND `friend_id` = '$user_id';";
		$result = mysql_query($query);
		$check_2 = mysql_num_rows($query);
		
		if($check_1 == 0 && $check_2 == 0 && $user_id != $friend_id)
		{
			$query = "insert into `friends` (`id`, `user_id`, `friend_id`) values (NULL, '".$user_id."', '".$friend_id."');";
			mysql_query($query);	
		}	
	}
	header('Location: ../users/homeboard.php?id='.$_GET['id']);
?>
